<?php
/**
 * MyBB 1.6
 * Copyright 2010 MyBB Group, All Rights Reserved
 *
 * Website: http://mybb.com
 * License: http://mybb.com/about/license
 *
 * $Id: mod.php 5380 2011-02-21 12:04:43Z Otse $
 */
 
// Disallow direct access to this file for security reasons
if(!defined("IN_MYBB"))
{
	die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}

$plugins->add_hook("postbit", "judge_postbit");
$plugins->add_hook("showthread_linear", "judge_showthread_linear");


function judge_info()
{
    return array(
	"name"			=> "Judge",
	"description"	=> "You can now judge posts.",
	"website"		=> "http://ak.fm",
	"author"		=> "Steelrat",
	"authorsite"	=> "http://ak.fm",
	"version"		=> "0.9",
	"guid" 			=> "",
	"compatibility" => "*"
    );

}

function judge_install()
{
    
}

function judge_uninstall()
{
    
}

function judge_is_installed()
{
    return true;
}

function judge_activate()
{
    
}


function judge_deactivate()
{
    
}

function judge_postbit(&$post)
{
    global $kutscript, $headerinclude, $mybb, $db, $templates, $lang, $theme;

    $lang->load("judge");
    
    if ( ! isset ( $kutscript ) ) $kutscript = 'JID=[];';
    
    
    if ( $_GET['jp'] == $post['pid'] && $mybb->user['uid'] != 0 )
    {
	if ( $post['uid'] != $mybb->user['uid'] )
	{
	    //$judge_quick = 'SELECT * FROM '.TABLE_PREFIX.'judge WHERE pid = '.$post['pid'].' AND uid = '.$mybb->user['uid'].'';
	    $result = $db->query('SELECT * FROM '.TABLE_PREFIX.'judge WHERE pid = '.$post['pid'].' AND uid = '.$mybb->user['uid'].'');
	    
	    if ( $db->num_rows( $result ) != 1 )
	    {
		$db->query('INSERT INTO '.TABLE_PREFIX.'judge VALUES(NULL, '.$post['pid'].', '.$mybb->user['uid'].', '.$_GET['j'].')');
	    }
	    else $judge_quick = $lang->judge_again;
	}
	else $judge_quick = $lang->judge_self;
    }
    
    $kutscript .= 'JID.push('.$post['pid'].');';
    
    //$judge_quick .= '<span class="judge_bit"><!--<img src="images/valid.gif">--> Agree x <strong>1</strong></span>';
    //$judge_quick .= '<span class="judge_bit"><!--<img src="images/invalid.gif">--> Disagree x <strong>2</strong></span>';

    $sql = '
SELECT mybb_judge.*, mybb_users.uid, mybb_users.username, mybb_users.usergroup FROM mybb_judge
LEFT JOIN mybb_users
ON mybb_judge.uid = mybb_users.uid
WHERE pid = '.$post['pid'].'
ORDER BY jid ASC
';
    
    //echo $sql;
    $result = $db->query($sql);
    
    $payne = array();//count($lang->judge_array));
    while( $array = $db->fetch_array($result) ) $payne[ $array['judgement'] ] ++;
    $c = 0;
    foreach ( $lang->judge_array as $i )
    {
	if ( $payne[$c] != 0 ) $judge_quick .= '<span class="judge_bit"><img src="'.$mybb->settings[bburl].'/images/judge/'.$i.'.gif"> '.$i.' x <strong>'.$payne[$c].'</strong></span>';
	$c ++;
    }
    if ( $c > 0 ) $judge_quick .= $lang->judge_quick;
    if ( $db->num_rows( $result ) == 0 && $judge_quick == null) $judge_quick = $lang->judge_quick;
    $db->data_seek($result, 0);
        
    $post['judge'] = '<div class="judge pane smalltext" id="judge'.$post['pid'].'">';
    
    $post['judge'] .= '<div>';
    $post['judge'] .= $lang->judge_you;
    $c = 0; foreach ( $lang->judge_array as $i )
	$post['judge'] .= '<span class="judge_bit"><img src="'.$mybb->settings[bburl].'/images/judge/'.$i.'.gif"> <a href="?tid='.$_GET['tid'].'&jp='.$post['pid'].'&j='.$c++.'#pid'.$post['pid'].'">'.$i.'</a></span>';
    $post['judge'] .= '</div>';
    
    
    $c = 0; foreach ( $lang->judge_array as $i )
    {
	if ( $payne[$c] != 0 )
	{
	    $margin = '0';
	    $d = $c;
	    while ( ++$d < count($lang->judge_array) )// array_key_exists ( ++$d, $payne) )
	    {
		if ( $payne[$d] > 0 ) $margin = '8';
	    }
	    $post['judge'] .= '<div class="judgebox" style="margin-right: '.$margin.'px;">';
	    $post['judge'] .= '<img src="'.$mybb->settings[bburl].'/images/judge/'.$i.'.gif"> '.$i.' x <strong>'.$payne[$c].'</strong>';
	    $post['judge'] .= '<ul>';
	    while( $array = $db->fetch_array($result) )
		if ( $array['judgement'] == $c )
		    $post['judge'] .= '<li><a href="member.php?action=profile&amp;uid='.$array['uid'].'">'.format_name($array['username'], $array['usergroup']).'</a></li>';
	    $post['judge'] .= '</ul>';
	    $post['judge'] .= '</div>';
	    
	    $db->data_seek($result, 0);
	}
	$c ++;
    }
    
    $post['judge'] .= '</div>';
    
    eval("\$post['judge_quick'] = \"".$templates->get("judge")."\";");
}

function judge_showthread_linear()
{
    global $pids, $kutscript, $headerinclude;
    
    $headerinclude .= "<script type=\"text/javascript\">$kutscript</script>";
}
